<template>
	<view class="container">
		<view class="header">
			<view class="title">

				  {{['绿色积分','绿豆','贡献值','权证'][data.wallet_type]}}
			</view>
			<view class="box"  @click="$goURl('/pages/personal/wallet/balance?wallet_type=0')">
				<view class="money">
					<image src="https://img.fubaozx.cn/static/oldImg/1699508886203.png" mode="aspectFill"></image>
					{{[$toFiexd(data.userInfo.money||0.00,2),data.userInfo.coupon,data.userInfo.contribution,data.userInfo.warrant][data.type]}}
				</view>
				<view class="btn"  v-if="data.transfer1.is_open" @click.stop="$goURl('/pages/personal/wallet/recharge?code=0')">
					转账
				</view>
				<view class="btn" v-if="data.cash.is_open"  @click.stop="$goURl('/pages/personal/wallet/withdrawal')">
					提现
				</view>
			</view>
		</view>
<!-- 		<view class="content">
			<view class="title">
				我的账户
			</view>
			<view class="list">

				<view class="item" @click.stop="$goURl('/pages/personal/wallet/balance?wallet_type=1')">
					<view class="left">
						<view class="text">
							积分
						</view>
						<view class="money">
							{{$toFiexd(data.userInfo.coupon||0.00,2)}}
						</view>
					</view>
					<view class="right" v-if="data.transfer2.is_open" @click.stop="$goURl('/pages/personal/wallet/recharge?code=1')">
						转账
					</view>
				</view>

			</view>

		</view> -->
		<!-- 明细 -->
					<view class="detail">
		<!-- 				<view class="detailHder flex justify-between align-center">
							<view class="flex align-center">
								<image src="https://pubuimg.cqguanjie.cn/chat/16948580779132"></image>
								<uni-data-select style="width: 45vw;"
								        v-model="data.value"
								        :localdata="data.range"
								        @change="change"
												placeholder="请选择类型"
								      ></uni-data-select>
							</view>
							<yearsPicker :defaultValue="modes()" @change="confirm" :end="data.endtime"></yearsPicker>
						</view> -->
						<!-- 空数据 -->
						<view class="empty" v-if="data.detailsList.length==0">
							<empty></empty>
						</view>
						<!-- 有钱包明细 -->
						<view class="moneys" v-else>
							<scroll-view style="height: 100%;" scroll-y @scrolltolower='records'>
								<view class="moneysFor" v-for="(item,index) in data.detailsList" :key="index">
									<view class="information">
										<text class="text" >{{data.walletList[item.detailed]}}</text>
										<view class="source">
											<!-- v-if="item.source.username" -->
											<!-- {{item.source.username}} -->
											<text v-if="item.source.username||item.source.mobile">来源：{{item.source.username||item.source.mobile}}</text>
										</view>
										<text class="date">{{$clearTime(item.CreatedAt)}}</text>
									</view>
									<view class="moneyl text-right">
										<text>{{$toFiexd(item.money,2)}}</text>
										<view>剩余:{{$toFiexd(item.change_money,2)}}</view>
									</view>
								</view>
							</scroll-view>
						</view>
					</view>
	</view>
</template>

<script setup>
	import {ref,reactive,getCurrentInstance} from "vue";
	import {onLoad,onShow,onPullDownRefresh,onReachBottom} from "@dcloudio/uni-app";
	const { proxy } = getCurrentInstance();
	const data = reactive({
		page:0,
		pages:1,
		size:10,
		type:1,
		value: -1,
		detailsList:[],//明细
		walletList:{},//钱包名称列表
		userInfo:{},
		cash:{},
		transfer1:{},
		transfer2:{},
	})
	onLoad((option)=>{
		data.type = option.type
		// data.walletList = option.type
	})
	onShow(()=>{
		data.detailsList=[]
		userinfo()
		data.walletList = uni.getStorageSync('wallet')
		console.log(data.walletList,"data.walletList")
	})
	onPullDownRefresh(()=> {
			setTimeout(function () {
				uni.stopPullDownRefresh();
			}, 1000);
		})
	onReachBottom(()=>{
		console.log('onReachBottom')
	})
	async function userinfo(){
		const res = await proxy.$requestPromise({
			url: `api/user_info/`,
			method: 'get',
		})
		data.userInfo = res.data
		uni.setStorageSync("userInfo",res.data)
		allocation()
		records()

	}
	function records(){
		if(data.page<data.pages){
			data.page++
			let detailed=[]
			if(data.value!=-1){
				 detailed=[data.value]
			}
			detailed=JSON.stringify(detailed)
			proxy.$request({
				url: `api/record/?page=${data.page}&size=${data.size}&wallet_type=${data.type}`,
				method: 'GET',
				success(res) {
					console.log(res,"res")
					let datas = res.data.data
					data.szData={
						totalMoney:(res.data.totalMoney-res.data.expenditure)>0?(res.data.totalMoney-res.data.expenditure):(res.data.totalMoney-res.data.expenditure)*-1,
						expenditure:res.data.expenditure,
					}

					datas.forEach((ct)=>{
						data.detailsList.push(ct)
					})
					if(res.data.data.length==data.size){
						data.pages++
					}
					// data.pages = Math.ceil(res.data.total / data.size)
					if (data.pages) {
						if(data.page==data.pages){
							data.loadmore = "nomore"
						}else{
							data.loadmore = "loading"
						}
					}else{
						data.loadmore = "nomore"
					}
				}
			})
		}
	}
	async function allocation(){
		const cash = await proxy.$requestPromise({
			url: `api/cash/`,
			method: 'PUT',

		})
		const transfer1 = await proxy.$requestPromise({
			url: `api/transfer/?code=0`,
			method: 'PUT'
		})
		const transfer2 = await proxy.$requestPromise({
			url: `api/transfer/?code=1`,
			method: 'PUT'
		})
		if(cash.errno==0){
			data.cash=cash.data
		}else{
			data.cash={
				is_open:false
			}
		}
		data.transfer1=transfer1.data
		data.transfer2=transfer2.data
	}
</script>

<style lang="scss" scoped>
	page{
	background-color: #FAFAFA;
	}
	.container{
		background-image: url('https://pili-vod.guanxikeji.com/moban/img/1719537707813.png');
		background-repeat: no-repeat;
		background-size: 100%;
		padding: 30upx 0;
		.header{
			margin: 0 30upx;
			padding: 30upx;
			background-color: #fff;
			border-radius: 30upx;
			box-shadow: 0px 3px 8px 0px rgba(0,0,0,0.08);
			.title{
				font-size: 28upx;
				font-weight: bold;
			}
			.box{
				display: flex;
				justify-content: space-between;
				margin-top: 20upx;
				align-items: center;
				.money{
					display: flex;
					align-items: center;
					font-size: 48upx;
					font-weight: bold;
					image{
						width: 70upx;
						height: 70upx;
						margin-right: 20upx;
					}
				}
				.btn{
					background-color: #F63B29;
					color: #fff;
					padding: 10upx 30upx;
					border-radius: 40upx;
					font-size: 24upx;
				}
			}
		}
		.content{
			padding: 30upx 15upx;
			.title{
				font-size: 32upx;
				font-weight: bold;
				padding:15upx ;
			}
			.list{
				display: flex;
				flex-wrap: wrap;
				.item{
					width: calc(50% - 90upx);
					margin: 15upx;
					background-color: #fff;
					box-shadow: 0px 3px 8px 0px rgba(0,0,0,0.08);
					padding: 30upx;
					border-radius: 20upx;
					display: flex;
					align-items: center;
					justify-content: space-between;
					.left{
						.text{
							font-size: 24upx;
							color: #ccc;
						}
						.money{
							font-size: 32upx;
							font-weight: bold;
							margin-top: 15upx;
						}
					}
					.right{
						color: $color1;
							font-size: 24upx;
					}
				}
			}
		}
	}
	.detail{
		width: 94%;
		background-color: #fff;
		margin: -40px auto 0 auto;
		padding: 20px 0;
		border-radius: 10px;
		.detailHder{
			width: 100%;
			image{
				width: 4px;
				height: 12px;
				margin: 0 4px 0 15px;
			}
			text{
				font-size:14px;
				// font-family: PingFang SC, PingFang SC-Bold;
				// font-weight: 700;
				// text-align: left;
				// color: #333333;
			}
		}
		.empty{
		}
		.moneys{
			width: 100%;
			height: calc(100vh - 400rpx);
			.moneysFor{
				display: flex;
				justify-content: space-between;
				align-items: center;
				border-bottom: 1px solid rgb(245, 245, 245);
				padding: 15px;
				.information{
					display: flex;
					flex-direction: column;
					.text{
						color: #333;
						font-weight: 700;
						font-size: 13px;
					}
					.source{
						text{
							color: rgb(153, 153, 153);
							font-size: 14px;
						}
					}
					.date{
						color: rgb(153, 153, 153);
						font-size: 14px;
					}
				}
				.moneyl{
					text{
						color: #F63B29;
						font-size: 16px;
						font-weight: 700;
					}
					view{
						font-size: 12px;
						color: #999;
					}
				}
			}
		}
	}
</style>
